package cn.xdmnb.cloud.blog.plugin.knife4j;

import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.spring.configuration.Knife4jAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yukai
 * @since 2023年01月06日 15:42
 */
@EnableConfigurationProperties(value = {Knife4jProperties.class})
@ConditionalOnProperty(prefix = "knife4j", name = {"enable"}, havingValue = "true", matchIfMissing = false)
@EnableSwagger2WebMvc
@Configuration
@AutoConfigureBefore(value = {Knife4jAutoConfiguration.class})
@Slf4j
public class Knife4jAutoConfig {

    @Autowired
    private Knife4jProperties knife4jProperties;

    @Bean
    public Docket defaultApi2(){
        log.debug("[Cloud-Blog-Plugins] Register the knife4j plugin, configuration information: {}",  JSONUtil.toJsonStr(knife4jProperties));
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(
                        new ApiInfoBuilder()
                                .title(knife4jProperties.getApiInfo().getTitle())
                                .description(knife4jProperties.getApiInfo().getDescription())
                                .termsOfServiceUrl(knife4jProperties.getApiInfo().getTermsOfServiceUrl())
                                .version(knife4jProperties.getApiInfo().getVersion())
                                .license(knife4jProperties.getApiInfo().getLicense())
                                .licenseUrl(knife4jProperties.getApiInfo().getLicenseUrl())
                                .contact(new Contact(knife4jProperties.getAuthor().getName(), knife4jProperties.getAuthor().getUrl(), knife4jProperties.getAuthor().getEmail()))
                                .build()
                )
                .groupName(knife4jProperties.getGroupName())
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .select()
                .apis(RequestHandlerSelectors.basePackage(knife4jProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }

    private List<SecurityScheme> securitySchemes()
    {
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization","Authorization" ,"Header" );
        result.add(apiKey);
        return  Collections.singletonList(apiKey);
    }
    /**
     * 授权信息全局应用
     */
    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(Collections.singletonList(new SecurityReference("Authorization", new AuthorizationScope[]{new AuthorizationScope("global", "Authorization")})))
                        .build()
        );
    }

}
